package org.dna.mybatis.criteria;

/**
 * Criterion for the like operator.
 * 
 * @author andrea
 */
public class LikeExpression implements Criterion {
    
    private String m_propName;
    private String m_value;
    private String m_columnName = null;
    
    LikeExpression(String propertyName, String value) {
        m_propName = propertyName;
        m_value = value;
    }

    public String getSql() {
        StringBuilder sb = new StringBuilder();
        sb.append("(");
        if (m_columnName == null) { //property not bound
            sb.append("< not bound:").append(m_propName).append(">");
        } else {
            sb.append(m_columnName);
        }
        sb.append(" LIKE '%").append(m_value).append("%')");
        return sb.toString();
    }

    public boolean verify(Criteria criteria) {
        return criteria.hasProperty(m_propName) && criteria.checkType(m_propName, String.class);
    }

    public void bind(Criteria crit) throws CriteriaException {
        m_columnName = crit.toColumnName(m_propName);
    }
}
